#include <iostream>
#include <vector>
#include <stack>
using namespace std;

class Solution {
public:
    int gcd(int a, int b) {
        while (b != 0) {
            int tmp = b;
            b = a % b;
            a = tmp;
        }
        return a;
    }

    int lcm(int a, int b) {
        int g = gcd(a, b);
        return (a / g) * b;
    }
    vector<int> replaceNonCoprimes(vector<int>& nums) {
        vector<int> ret;
        for (int x : nums) {
            while (ret.size() && gcd(x, ret.back()) > 1) {
                x = lcm(x, ret.back());
                ret.pop_back();
            }
            ret.push_back(x);
        }
        return ret;
    }
};

int main()
{
    Solution s;
    vector<int> v({ 6,4,3,2,7,6,2 });
    s.replaceNonCoprimes(v);
    return 0;
}